/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent.action;

import java.util.List;

import ca.usherbrooke.agent.SimpleMoveAgentController;
import ca.usherbrooke.application.astar.action.AStarMoveAction;
import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.model.world.IWorldModel;
import ca.usherbrooke.tools.position.Vector2D;
import ca.usherbrooke.view.slick.jade.worldView.shared.EntityViewDebug;

/**
 * @author Cody Stoutenburg
 * 
 */
public class MoveAction extends Action {
	private static final long serialVersionUID = 1L;

	private final Integer firstIdx;
	private final Integer secondIdx;

	// values generated by vals
	private Vector2D destination;

	public MoveAction(Long duration, Integer firstIdx) {
		super(duration);
		this.firstIdx = firstIdx;
		this.secondIdx = firstIdx + 1;
	}

	protected Boolean verifyArgs(List<Float> vals) {
		if (firstIdx >= vals.size() || secondIdx >= vals.size()) {
			return false;
		}

		return true;
	}

	@Override
	public Boolean init(List<Float> vals) {
		if (!verifyArgs(vals)) {
			return false;
		}

		destination = new Vector2D(vals.get(firstIdx), vals.get(secondIdx));
		return true;
	}

	@Override
	protected Boolean canDoAction() {
		IWorldModel world = this.agentController.getKnownWorld();
		IEntityModel entity = this.agentController.getEntity();

		if (entity == null) {
			return false;
		}

		if (destination.getX() > world.getSize().getWidth()) {
			return false;
		}
		if (destination.getY() > world.getSize().getHeight()) {
			return false;
		}

		if (world.getWall(destination) != null) {
			return false;
		}

		if (entity.getSize().contains(destination.getX(), destination.getY())) {
			return false;
		}

		// we generate a simple and fast plan
		return Action.canReachTargetPosition(agentController, destination);
	}

	@Override
	public void apply() {
		Long guid = this.agentController.getEntity().getGuid();

		EntityViewDebug.INSTANCE.setTargetPosition(guid, destination);

		List<AStarMoveAction> plan = Action.generatePlanToTargetPosition(
				agentController, destination);
		agentController.setPlan(plan);
	}

	@Override
	public Boolean isFinish() {
		IEntityModel entity = this.agentController.getEntity();

		if (entity.getSize().contains(destination.getX(), destination.getY())) {
			return true;
		}

		return false;
	}

	public String canDoActionInfo(List<Float> vals,
			SimpleMoveAgentController controller) {

		IWorldModel world = controller.getWorld();
		IEntityModel model = this.agentController.getEntity();
		if (model == null) {
			return "no entity found";
		}

		if (firstIdx >= vals.size() || secondIdx >= vals.size()) {
			return "invalid index";
		}

		destination = new Vector2D(vals.get(firstIdx), vals.get(secondIdx));

		if (destination.getX() > world.getSize().getWidth()) {
			return "invalid destination x";
		}
		if (destination.getY() > world.getSize().getHeight()) {
			return "invalid destination y " + "[current " + destination.getY()
					+ "]" + "[max " + world.getSize().getHeight() + "]";
		}

		if (world.getWall(destination) != null) {
			return "invalid destion is a wall";
		}

		if (model.getSize().contains(destination.getX(), destination.getY())) {
			return "invalid destination, destination already reach";
		}

		return "can reach destination";
	}

	@Override
	public String toString() {
		String dest = "destination: ";
		if (destination != null) {
			dest += destination.toString();
		} else {
			dest += "none";
		}
		return dest;
	}

	@Override
	public Float generateEstimatedCost() {
		return this.agentController.getEntity().getCenterPosition()
				.getDistance(destination);
	}

	@Override
	public Float generateEstimatedWon() {
		return 1.0f;
	}
}
